package com.mall.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.model.AdminUser;
import com.mall.service.AdminUserService;
import com.mall.util.MyUtil;

@Controller
@RequestMapping("/AdminUserContorller")
public class AdminUserContorller extends MyUtil {
	@Autowired
	private AdminUserService adminUserService;

	/**
	 * 管理员登录
	 *
	 * @dagewang 2017年9月25日
	 */
	@RequestMapping("/adminlogin")
	public String login(String username, String password, HttpSession session) {
		if (!this.isNull(username, password)) {
			AdminUser au = adminUserService.getByName(username, password);
			if (au != null) {
				au.setUsername(null);
				au.setPassword(null);
				session.setAttribute("adminuser", au);
				return "system";
			}
		}
		return "adminLogin";

	}

	/**
	 * 修改管理员密码
	 *
	 * @dagewang 2017年8月23日
	 */
	@RequestMapping("/update")
	public @ResponseBody Map<String, Object> update(String password, String oldPwd, HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", null);
		if (password == null || password.equals("") || oldPwd == null || oldPwd.equals("")) {
			return map;
		}
		AdminUser admin = (AdminUser) session.getAttribute("adminuser");
		if (admin == null) {
			return map;
		}
		boolean result = adminUserService.update(password, oldPwd, admin.getId());
		if (result) {
			map.put("result", 1);
		}
		return map;
	}
	/**
	 * 退出登录
	 *
	 *@dagewang 2017年10月9日
	 */
	@RequestMapping("/outLogin")
	public String outLogin(HttpSession session){
		session.setAttribute("adminuser",null);
		return "adminLogin";
	}
	public AdminUserService getAdminUserService() {
		return adminUserService;
	}

	public void setAdminUserService(AdminUserService adminUserService) {
		this.adminUserService = adminUserService;
	}

}
